/**
 * Created by liujinghao on 12/14 0014.
 *
 */
    //  中国地图
    var data = [
        {name: '海门', value: 90},
        {name: '鄂尔多斯', value: 12},
        {name: '招远', value: 12},
        {name: '舟山', value: 12},
        {name: '齐齐哈尔', value: 14},
        {name: '盐城', value: 15},
        {name: '赤峰', value: 16},
        {name: '青岛', value: 18},
        {name: '乳山', value: 18},
        {name: '金昌', value: 19},
        {name: '泉州', value: 150},
        {name: '莱西', value: 21},
        {name: '日照', value: 21},
        {name: '胶南', value: 22},
        {name: '南通', value: 23},
        {name: '拉萨', value: 24},
        {name: '云浮', value: 24},
        {name: '梅州', value: 25},
    ];
var geoCoordMap = {
    '海门': [121.15, 31.89],
    '鄂尔多斯': [109.781327, 39.608266],
    '招远': [120.38, 37.35],
    '舟山': [122.207216, 29.985295],
    '齐齐哈尔': [123.97, 47.33],
    '盐城': [120.13, 33.38],
    '赤峰': [118.87, 42.28],
    '青岛': [120.33, 36.07],
    '乳山': [121.52, 36.89],
    '金昌': [102.188043, 38.520089],
    '泉州': [118.58, 24.93],
    '莱西': [120.53, 36.86],
    '日照': [119.46, 35.42],
    '胶南': [119.97, 35.88],
    '南通': [121.05, 32.08],
    '拉萨': [91.11, 29.97],
    '云浮': [112.02, 22.93],
    '梅州': [116.1, 24.55],
};

var convertData = function (data) {
    var res = [];
    for (var i = 0; i < data.length; i++) {
        var geoCoord = geoCoordMap[data[i].name];
        if (geoCoord) {
            res.push({
                name: data[i].name,
                value: geoCoord.concat(data[i].value)
            });
        }
    }
    return res;
};

function optionChinaCalc() {
    option_china = {
        tooltip: {
            show: true,
            "formatter": function (params) {
                var obj = JSON.parse(JSON.stringify(params));
                var str = obj.name + ":" + obj.data.value[2];
                return str;
            }
        },
        geo: {
            map: 'china',
            label: {
                emphasis: {
                    show: false
                }
            },
            roam: false,
            itemStyle: {
                normal: {
                    areaColor: 'rgba(0,0,0,0.5)',
                    borderColor: '#fff'
                },
                emphasis: {
                    areaColor: 'rgba(0,0,0,0.5)'
                }
            }
        },
        series: [
            {
                name: 'pm2.5',
                type: 'scatter',
                coordinateSystem: 'geo',
                data: convertData(data),
                symbolSize: function (val) {
                    return val[2] / 10;
                },
                label: {
                    normal: {
                        formatter: '{b}',
                        position: 'right',
                        show: false,
                        textStyle: {
                            color: '#d91c25'
                        }
                    },
                    emphasis: {
                        show: true
                    }
                },
                itemStyle: {
                    normal: {
                        color: '#d91c25'
                    }
                }
            },
            {
                name: 'Top 5',
                type: 'effectScatter',
                coordinateSystem: 'geo',
                data: convertData(data.sort(function (a, b) {
                    return b.value - a.value;
                }).slice(0, 6)),
                symbolSize: function (val) {
                    return val[2] / 5;
                },
                showEffectOn: 'render',
                rippleEffect: {
                    brushType: 'stroke'
                },
                hoverAnimation: false,
                itemStyle: {
                    normal: {
                        color: '#d91c25',
                        shadowBlur: 10,
                        shadowColor: '#333'
                    }
                },
                zlevel: 1
            }
        ]
    };
}
optionChinaCalc();
    var chinaDiv = echarts.init(document.getElementById('chinaDiv'));
    chinaDiv.setOption(option_china);
// function setStay(para) {
//      data=para.map.data;
//     geoCoordMap=para.map.coords;
//     optionChinaCalc();
//     var chinaDiv = echarts.init(document.getElementById('chinaDiv'));
//     chinaDiv.setOption(option_china);
// }

//本地游客逗留时长
function createColor(max) {
    var tpl=['#099fac', '#0b6b73'];
    var temp=[];
    for(var i=0;i<max;i++){
        temp=temp.concat('#099fac', '#0b6b73')
    }
    return temp
}
var color100= createColor(100)
option_resident_local = {
    textStyle: {
        color: 'rgba(255, 255, 255, 0.8)'
    },
    tooltip: {
        trigger: 'axis',
        axisPointer: {
            type: 'shadow'
        },
        formatter: "{a} <br/> {b} : {c}小时"
    },
    grid: {
        top: 15,
        left: '2%',
        right: '3%',
        bottom: '10%',
        containLabel: true
    },
    xAxis: [
        {
            type: 'category',
            data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
        }
    ],
    yAxis: [
        {
            splitLine: {
                show: true,
                lineStyle: {
                    // 使用深浅的间隔色
                    color: ['#2f2f2f']
                }
            },
            type: 'value'
        }
    ],
    dataZoom: [
        {
            type: 'slider', 
            zoomOnMouseWheel: true,
            show: true,
            start: 0,
            end: 35,
            bottom: 0,
            textStyle: {
                color: "#fff"
            }
        }
    ],
    series: [
        {
            name: '逗留时长',
            type: 'bar',
            barWidth: '30%',
            data: [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3],
            itemStyle: {
                normal: {
                    color: function (params) {
                        // build a color map as your need.
                        var colorList = color100.slice(0)
                        return colorList[params.dataIndex]
                    }, label: {
                        show: false,
                        position: 'top',
                        formatter: '{c}'
                    }
                }
            }
        }
    ]
};

var residentLocalDiv = echarts.init(document.getElementById('residentLocalDiv'));
residentLocalDiv.setOption(option_resident_local);
function setLocal(para) {
    option_resident_local.xAxis[0].data=para.local.name;
    option_resident_local.series[0].data=para.local.value;
    var residentLocalDiv = echarts.init(document.getElementById('residentLocalDiv'));
    residentLocalDiv.setOption(option_resident_local);
}

// 外地游客逗留
option_resident_province= {
    textStyle: {
        color: 'rgba(255, 255, 255, 0.8)'
    },
    tooltip: {
        trigger: 'axis',
        axisPointer: {
            type: 'shadow'
        },
        formatter: "{a} <br/> {b} : {c}小时"
    },
    grid: {
        top: 15,
        left: '2%',
        right: '3%',
        bottom: '10%',
        containLabel: true
    },
    xAxis: [
        {
            type: 'category',
            data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
        }
    ],
    yAxis: [
        {
            splitLine: {
                show: true,
                lineStyle: {
                    // 使用深浅的间隔色
                    color: ['#2f2f2f']
                }
            },
            type: 'value'
        }
    ],
    dataZoom: [
        {
            type: 'slider', 
            zoomOnMouseWheel: true,
            show: true,
            start: 0,
            end: 35,
            bottom: 0,
            textStyle: {
                color: "#fff"
            }
        }
    ],
    series: [
        {
            name: '逗留时长',
            type: 'bar',
            barWidth: '30%',
            data: [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3],
            itemStyle: {
                normal: {
                    color: function (params) {
                        // build a color map as your need.
                        var colorList = color100.slice(0)
                        return colorList[params.dataIndex]
                    }, label: {
                        show: false,
                        position: 'top',
                        formatter: '{c}'
                    }
                }
            }
        }
    ]
};

var residentProvinceDiv = echarts.init(document.getElementById('residentProvinceDiv'));
    residentProvinceDiv.setOption(option_resident_province);
    
function setProvince(para) {
    option_resident_province.xAxis[0].data = para.province.name;
    option_resident_province.series[0].data = para.province.value;
    var residentProvinceDiv = echarts.init(document.getElementById('residentProvinceDiv'));
    residentProvinceDiv.setOption(option_resident_province);
}



function staySearch(t1, t2) {
    var opt = {
        type: 'get',
        url: '/bigdata/manage/portrait/stay-time',
        data: {},
        callback: function (res) {
            if (res.code == 1) {
                dyw.box.alert(res.msg);
                return false
            }
            $("#date1").val(res.result.start_time);
            $("#date2").val(res.result.end_time);
            setStay(res.result)
            setLocal(res.result)
            setProvince(res.result)
        }
    }
    if (t1) {
        opt.data = {
            start_time: t1,
            end_time: t2
        }
    }
    loadData(opt)
}
staySearch();
$("#search1").on('click', function () {
    var t1 = $("#date1").val();
    var t2 = $("#date2").val();
    if (!t1 || !t2) {
        dyw.box.alert('请选择查询时间');
        return false
    }
    staySearch(t1, t2);
})